<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <!--    <link rel="stylesheet" href="../portal/style/admin.css" media="all">-->
    <!--    <link id="layuicss-layer" rel="stylesheet" href="../layui/css/modules/layer/default/layer.css?v=3.1.1" media="all">-->
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<h2>RSA DEMO</h2>
<table>
    <tr>
        <td>流水号</td>
        <td><input type="text" name="requestNo" id="requestNo"/></td>
    </tr>
    <tr>
        <td>手机号</td>
        <td><input type="text" name="phone" id="phone"/></td>
    </tr>
    <tr>
        <td><input type="button" value="提交" onclick="post_data()"></td>
    </tr>
</table>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="base/rsa.js" type="text/javascript"></script>
<script src="base/jsrsasign-all-min.js" type="text/javascript"></script>
<script>
    //定义公钥
    var public_key = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXudMSXF9omT79vMfw9HvWQoQC4s7jxs1fI+x8cY5haySJgLPc+JV1Ub3meQm3kw+sZY051zKxbkUFpNZ4UGtVbjlzqVBLDEx5WH5MLFfjVylkbsSUj3+Ytbt/mxJ1zUBz/HAN/81fljVeFixbxsuQjpIhRLNwFQKthuH2ZlLPEwIDAQAB-----END PUBLIC KEY-----";
    var private_key ="-----BEGIN PRIVATE KEY-----MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJe50xJcX2iZPv28x/D0e9ZChALizuPGzV8j7HxxjmFrJImAs9z4lXVRveZ5CbeTD6xljTnXMrFuRQWk1nhQa1VuOXOpUEsMTHlYfkwsV+NXKWRuxJSPf5i1u3+bEnXNQHP8cA3/zV+WNV4WLFvGy5COkiFEs3AVAq2G4fZmUs8TAgMBAAECgYB8jaegYwRPsb3sPpVEUO94LJKCa/67LfBaMftpDS98+Z+84VLGC6tl6/ygvqwjby5mr9cHrImI7qTqxS7rBdmVupXLVMOSRvp00vWezEc+JotFpM6rsdHRFXcQTIPLQ+v7/iLuUGbV7eW5JWoNNKYegioxJOs6YsOspoJ9P3aEwQJBANxZWuci5FcS13+wSctOz+mdXkzfRQIw3Pi1XE1A3If2U0IykFysKsHrwdXZNlpt3smgiKB4/X/BtUCRHNNGiycCQQCwRin9muLeu82PXKz8ZVaqJInEdYF+Ui8CJx1PeWYSb4xLZPupMidx+zwxiiPx44+Luc/+uwp6xLjiA3rkiAA1AkBc2O373PD4W8EgVckSePgUyTG2FJJAdRR3W/wztTP3umykxSedVRKDOfMoR+mmOwuYTCOl8rVmgtn8BelugRgbAkA+Q3pfjw8l17ZjNjwQJwd2r9NN5qen5AWgwAo1YU8xod8vXmsaN5IBS9nWMdJChbvVgfu7IXwxIz2e79W/1V4lAkEAysWzLU2u3YFiPCseNZKahzfyg+D+JioZ6rmz8QaQChreiyPqRgS+pFdPRSjmdGPqbbY1tRHq8XwfuEsaBOC+TQ==-----END PRIVATE KEY-----";

    //登录
    function post_data() {
        //需要转换的json参数对象
        var body = {
            phone: $("#phone").val(),
            requestNo: $("#requestNo").val()
        };
        console.log("body=====" + JSON.stringify(body));
        var param = RSAEncryptByPre(JSON.stringify(body), private_key);
        console.log("数据私钥签名后=====" + param);
        //通过私钥进行数据解密
        var msg = RSADecryptByPub(JSON.stringify(body),param, public_key);
        console.log("数据公钥验签后数据=====" + msg);
        // 但是在后端加密的时候没有，解密就有那个鬼东西,所有我通过截取把那个鬼东西去掉了
        var dataJson = msg.substr(msg.indexOf("{"), msg.length);
        //通过截取数据，不知道为什么后端数据加密后，前端解密出现个无法识别的字符，
        console.log("截取后的数据=====" + dataJson)
        //转换成json
        let parse = JSON.parse(dataJson);
        console.log("转换成json后获取数据requestNo==" + parse.requestNo+"====phone=="+parse.phone);
    }
</script>
</body>
</html>
